<template>
  <div class="app-container product_body">
    <PRODUCT_TYPE class="product_type" :value="queryParams.typeId" @input="handleChoiceType"/>
    <div class="product_list">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                     v-hasPermi="['cms:jiayi_product:add']">新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                     v-hasPermi="['cms:jiayi_product:edit']">修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                     v-hasPermi="['cms:jiayi_product:remove']">删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                     v-hasPermi="['cms:jiayi_product:export']">导出
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getListPage"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="jiayi_productList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="海报图" align="center" prop="poster" width="100">
          <template slot-scope="scope">
            <imagePreview :src="scope.row.poster" width="100px" height="100px"/>
          </template>
        </el-table-column>
        <el-table-column label="名称" align="center" prop="name"/>
        <el-table-column label="类型" align="center" prop="typeDTO.name" width="80"/>
        <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['cms:jiayi_product:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['cms:jiayi_product:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                  @pagination="getListPage"/>
    </div>


    <!-- 添加或修改产品展示对话框 -->
    <el-drawer :title="title" :visible.sync="open" size="70%" direction="rtl" append-to-body destroy-on-close :wrapperClosable="false">
      <div class="app-container">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="top">
          <div class="form_flex">
            <section class="form_flex_left">
              <el-form-item label="名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入名称" style="width: 90%" clearable/>
              </el-form-item>
              <el-form-item label="类型" prop="typeId">
                  <el-cascader placeholder="请选择类型" v-model="form.typeId" :options="jiayi_product_typeList" :props="{ checkStrictly: true,value:'id',label: 'name',emitPath: false}" clearable style="width: 90%"/>
              </el-form-item>
              <el-form-item label="海报图" prop="poster">
                <ImageUpload :value="form.poster" :limit="1" @input="handleUploadPoster"/>
              </el-form-item>
            </section>
            <section class="form_flex_right">
              <el-form-item label="详细描述" prop="info">
                <editor v-model="form.info" height="70vh" style="border: 1px #DCDFE6 solid;width: 50vw;"/>
              </el-form-item>
            </section>
          </div>
        </el-form>
      </div>
      <div style="width: 100%;text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {
  listPageJiayi_product,
  getJiayi_product,
  delJiayi_product,
  addJiayi_product,
  updateJiayi_product
} from "@/api/cms/jiayi_product";
import {listJiayi_product_type} from "@/api/cms/jiayi_product_type";

//产品类型
import PRODUCT_TYPE from "@/views/cms/jiayi_product_type"

export default {
  name: "Jiayi_product",
  components: {PRODUCT_TYPE},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品展示表格数据
      jiayi_productList: [],
      jiayi_product_typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        typeId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getListPage();
  },
  methods: {
    /** 选择类型 */
    handleChoiceType(typeId){
      this.queryParams.typeId = typeId;
      this.handleQuery();
    },
    /** 查询产品展示列表 */
    getListPage() {
      this.loading = true;
      listPageJiayi_product(this.queryParams).then(response => {
        this.jiayi_productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        typeId: null,
        poster: null,
        info: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 查询产品展示类型列表 */
    getTypeListPage() {
      listJiayi_product_type({}).then(response => {
        this.jiayi_product_typeList = this.handleTree(response.data);
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getListPage();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.typeId = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTypeListPage();
      this.open = true;
      this.title = "添加产品展示";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTypeListPage();
      const id = row.id || this.ids
      getJiayi_product(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品展示";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateJiayi_product(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getListPage();
            });
          } else {
            addJiayi_product(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getListPage();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(`是否确认删除产品展示【${row.name}】吗？`).then(function () {
        return delJiayi_product(ids);
      }).then(() => {
        this.getListPage();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cms/jiayi_product/export', {
        ...this.queryParams
      }, `jiayi_product_${new Date().getTime()}.xlsx`)
    },
    //上传海报图
    handleUploadPoster(url){
      this.form.poster = url;
    }
  }
};
</script>
<style scoped>
.product_body{
  width: 100%;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
.product_type{
  width: auto;
}
.product_list{
  flex: 1;
}

.form_flex{
  width: 100%;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
}
.form_flex_left{
  width: 20%;
  display: flex;
  flex-direction: column;
  justify-content: start;
}
.form_flex_button{
  flex: 1;
}
</style>
